/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.EJB.gui;

import it.polimi.meteocal.EJB.control.EventDTO;
import it.polimi.meteocal.EJB.entities.Event;
import it.polimi.meteocal.EJB.entities.User;
import it.polimi.meteocal.EJB.sessionBeans.EventManager;
import it.polimi.meteocal.EJB.sessionBeans.UserManager;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.json.stream.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 *
 * @author Marianna
 */
@ManagedBean(name = "cb")
@ViewScoped
public class CalendarBean {
    
    @EJB
    private EventManager em;
    
    public CalendarBean(){}
    
    public List<String> getEvents(String a) throws IOException{
        
        List<String> stringEvents = new ArrayList<String>();
        
        for(Event e: em.allUserEvents(a)){
            EventDTO ed = new EventDTO();
            ed.setTitle(e.getTitle());
            
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String one = df.format(e.getDate());
            DateFormat de = new SimpleDateFormat("HH:mm:ss");
            String two = de.format(e.getBeginningHour());
            String three = de.format(e.getEndingHour());
            
            ed.setStart(one + "T" + two);
            ed.setEnd(one + "T" + three);
            ed.setUrl("/MeteoCal/webapp/user/event.xhtml?id=" + e.getId());
            
            if(e.getOrganizer().getUsername()==a){
                ed.setColor("indianred");
            }
            else if(!em.invitePerEvent(a, e.getId()).isEmpty()){
                if(em.invitePerEvent(a, e.getId()).get(0)){
                    ed.setColor("green");
                }
                else if(!em.invitePerEvent(a, e.getId()).get(0)) ed.setColor("red");
            }
            
            ObjectMapper mapper = new ObjectMapper();

            try{
                stringEvents.add(mapper.writeValueAsString(ed));
            }
            catch (JsonGenerationException ex) {
		ex.printStackTrace();
	} catch (JsonMappingException ex) {
		ex.printStackTrace();
	} catch (IOException ex) {
		ex.printStackTrace();
	}
            
            
        }
        
        return stringEvents;
    }
    
        public List<String> getOUEvents(String a) throws IOException{
        
        List<String> stringEvents = new ArrayList<String>();
            
        for(Event e: em.allUserEvents(a)){
            
            ///////////
            if(!e.getPrivate1()){
            
            EventDTO ed = new EventDTO();
            ed.setTitle(e.getTitle());
            
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String one = df.format(e.getDate());
            DateFormat de = new SimpleDateFormat("HH:mm:ss");
            String two = de.format(e.getBeginningHour());
            String three = de.format(e.getEndingHour());
            
            ed.setStart(one + "T" + two);
            ed.setEnd(one + "T" + three);
            ed.setUrl("/MeteoCal/webapp/user/event.xhtml?id=" + e.getId());
           
            if(e.getOrganizer().getUsername().equals(a)){
                ed.setColor("indianred");
            }
            else if(!em.invitePerEvent(a, e.getId()).isEmpty()){
                if(em.invitePerEvent(a, e.getId()).get(0)){
                    ed.setColor("green");
                }
                else if(!em.invitePerEvent(a, e.getId()).get(0)) ed.setColor("red");
            }
            
            ObjectMapper mapper = new ObjectMapper();

            try{
                stringEvents.add(mapper.writeValueAsString(ed));
            }
            catch (JsonGenerationException ex) {
		ex.printStackTrace();
	} catch (JsonMappingException ex) {
		ex.printStackTrace();
	} catch (IOException ex) {
		ex.printStackTrace();
	}
            }
            /////////
            if(e.getPrivate1()){
            EventDTO ed = new EventDTO();
            
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String one = df.format(e.getDate());
            DateFormat de = new SimpleDateFormat("HH:mm:ss");
            String two = de.format(e.getBeginningHour());
            String three = de.format(e.getEndingHour());
            
            ed.setStart(one + "T" + two);
            ed.setEnd(one + "T" + three);
            
            if(e.getOrganizer().getUsername().equals(a)){
                ed.setColor("indianred");
            }
            else if(!em.invitePerEvent(a, e.getId()).isEmpty()){
                if(em.invitePerEvent(a, e.getId()).get(0)){
                    ed.setColor("green");
                }
                else if(!em.invitePerEvent(a, e.getId()).get(0)) ed.setColor("red");
            }
            
            ObjectMapper mapper = new ObjectMapper();

            try{
                stringEvents.add(mapper.writeValueAsString(ed));
            }
            catch (JsonGenerationException ex) {
		ex.printStackTrace();
	} catch (JsonMappingException ex) {
		ex.printStackTrace();
	} catch (IOException ex) {
		ex.printStackTrace();
	}
            }
            
        }
        
        return stringEvents;
    }
        
    
}
